package cn.edu.nju.askme.service.impl;

import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import cn.edu.nju.askme.dao.IOptionDAO;
import cn.edu.nju.askme.dao.IPaperDAO;
import cn.edu.nju.askme.dao.IQuestionDAO;
import cn.edu.nju.askme.model.Paper;
import cn.edu.nju.askme.model.Question;
import cn.edu.nju.askme.model.User;
import cn.edu.nju.askme.service.IPaperService;

@Component
public class PaperService implements IPaperService {
	private IPaperDAO paperDAO;
	private IQuestionDAO questionDAO;
	private IOptionDAO optionDAO;
	public IPaperDAO getPaperDAO() {
		return paperDAO;
	}

	@Resource
	public void setPaperDAO(IPaperDAO paperDAO) {
		this.paperDAO = paperDAO;
	}

	public IQuestionDAO getQuestionDAO() {
		return questionDAO;
	}
	@Resource
	public void setQuestionDAO(IQuestionDAO questionDAO) {
		this.questionDAO = questionDAO;
	}

	public IOptionDAO getOptionDAO() {
		return optionDAO;
	}
	@Resource
	public void setOptionDAO(IOptionDAO optionDAO) {
		this.optionDAO = optionDAO;
	}

	public void add(Paper paper) {
		paperDAO.save(paper);
	}

	public Paper checkById(int id) {
		System.out.println(id);
		Paper p = paperDAO.findById(id);
		System.out.println(p);
		if(p==null)
			return p;
		List<Question> qList=questionDAO.findByProperty("paper", p);
		for(Question q:qList) {
			q.setOptions(optionDAO.findByProperty("question", q));
		}
		p.setQuestions(qList);
		return p;
	}

	public List<Paper> checkByCreator(User user) {
		return paperDAO.findByUser(user);
	}

	public List<Paper> checkAll() {
		return paperDAO.findAll();
	}

}
